using System;
using Gadgeteer.Interfaces.Led;
using Gadgeteer.Interfaces.Fakes.Framework;

namespace Gadgeteer.Interfaces.Fakes
{
    public class FakeMultiColorLed : IMultiColorLed
    {
        private Color color;

        private readonly Color[] allowedColors;

        public FakeMultiColorLed()
            : this(new Color[] {})
        {
        }

        public FakeMultiColorLed(params Color[] allowedColors)
        {
            color = Color.Black;
            this.allowedColors = allowedColors;
        }

        public Color Color
        {
            get { return color; }
        }

        public bool IsOn
        {
            get { return color != Color.Black; }
        }

        public void TurnBlue()
        {
            TurnColor(Color.Blue);
        }

        public void TurnGreen()
        {
            TurnColor(Color.Green);
        }

        public void TurnRed()
        {
            TurnColor(Color.Red);
        }

        public void TurnWhite()
        {
            TurnColor(Color.White);
        }

        public void TurnColor(Color color)
        {
            if (allowedColors.Length != 0)
            {
                for (int i = 0; i < allowedColors.Length; i++)
                {
                    if (allowedColors[i] == color)
                    {
                        this.color = color;
                        return;
                    }
                }
                throw new InvalidOperationException("That color is not available");
            }
            this.color = color;
        }

        public void TurnOff()
        {
            TurnColor(Color.Black);
        }
    }
}
